/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewPedidos.java
 *
 * Created on 02/12/2011, 20:00:05
 */
package View;

import Controle.ClienteCtr;
import Controle.PedidosCtr;
import Controle.ProdutoCtr;
import Model.ProdutoModel;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Diego
 */
public class ViewPedidos extends javax.swing.JDialog {

    /** Creates new form ViewPedidos */
    public ViewPedidos(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Vector<String> ColunasTabela = new Vector<String>();
        ColunasTabela = PegaColunasDaGrade();
        DefaultTableModel MinhaTabela = new DefaultTableModel(ColunasTabela, 0);
        jTable1.setModel(MinhaTabela);
        TrocaEstado(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        TxtCodigo = new javax.swing.JTextField();
        BtnPrimeiro = new javax.swing.JButton();
        BtnAnterior = new javax.swing.JButton();
        BtnProximo = new javax.swing.JButton();
        BtnUltimo = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        TxtNomeCli = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        TxtCodCli = new javax.swing.JTextField();
        BtnPesquisar = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        BtnNovo = new javax.swing.JButton();
        BtnSalvar = new javax.swing.JButton();
        BtnCancelar = new javax.swing.JButton();
        BtnExcluir = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel1.setText("Código");

        TxtCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TxtCodigoActionPerformed(evt);
            }
        });
        TxtCodigo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                TxtCodigoFocusLost(evt);
            }
        });
        TxtCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TxtCodigoKeyPressed(evt);
            }
        });

        BtnPrimeiro.setText("<<");
        BtnPrimeiro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnPrimeiroActionPerformed(evt);
            }
        });

        BtnAnterior.setText("<");
        BtnAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnAnteriorBtnPrimeiroActionPerformed(evt);
            }
        });

        BtnProximo.setText(">");
        BtnProximo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnProximoBtnPrimeiroActionPerformed(evt);
            }
        });

        BtnUltimo.setText(">>");
        BtnUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnUltimoBtnPrimeiroActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(TxtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 194, Short.MAX_VALUE)
                .addComponent(BtnPrimeiro, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BtnAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BtnProximo, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BtnUltimo, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(TxtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BtnAnterior)
                    .addComponent(BtnProximo)
                    .addComponent(BtnUltimo)
                    .addComponent(BtnPrimeiro))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), "Dados Pessoais"));

        jLabel2.setText("Nome");

        TxtNomeCli.setEditable(false);
        TxtNomeCli.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TxtNomeCliKeyPressed(evt);
            }
        });

        jLabel4.setText("Codigo");

        TxtCodCli.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                TxtCodCliFocusLost(evt);
            }
        });
        TxtCodCli.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TxtCodCliKeyPressed(evt);
            }
        });

        BtnPesquisar.setText("...");
        BtnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnPesquisarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TxtCodCli, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(BtnPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TxtNomeCli, javax.swing.GroupLayout.PREFERRED_SIZE, 382, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TxtNomeCli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(TxtCodCli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(BtnPesquisar))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        jPanel3.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        BtnNovo.setText("Novo");
        BtnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnNovoActionPerformed(evt);
            }
        });

        BtnSalvar.setText("Salvar");
        BtnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnSalvarActionPerformed(evt);
            }
        });

        BtnCancelar.setText("Cancelar");
        BtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCancelarActionPerformed(evt);
            }
        });

        BtnExcluir.setText("Excluir");
        BtnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnExcluirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(BtnNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BtnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BtnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BtnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(128, Short.MAX_VALUE))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {BtnCancelar, BtnExcluir, BtnNovo, BtnSalvar});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtnNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BtnSalvar)
                    .addComponent(BtnCancelar)
                    .addComponent(BtnExcluir))
                .addContainerGap())
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {BtnCancelar, BtnExcluir, BtnNovo, BtnSalvar});

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), "Itens do Pedido"));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTable1MousePressed(evt);
            }
        });
        jTable1.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                jTable1AncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTable1KeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 568, Short.MAX_VALUE)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 187, Short.MAX_VALUE)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(28, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void TxtCodigoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_TxtCodigoFocusLost
}//GEN-LAST:event_TxtCodigoFocusLost

    private void TxtCodigoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TxtCodigoKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            TxtNomeCli.requestFocus();
        }
}//GEN-LAST:event_TxtCodigoKeyPressed

    private void BtnPrimeiroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnPrimeiroActionPerformed
        int opcao = -1;
        int CodAtual = 0;
        if (evt.getSource() == BtnPrimeiro) {
            opcao = 0;
        } else if (evt.getSource() == BtnAnterior) {
            opcao = 1;
        } else if (evt.getSource() == BtnProximo) {
            opcao = 2;
        } else if (evt.getSource() == BtnUltimo) {
            opcao = 3;
        }
        try {
            CodAtual = Integer.parseInt(TxtCodigo.getText());
        } catch (Exception e) {
            CodAtual = 0;
        }
        PreencherCamposdaTela(CodAtual, opcao);
}//GEN-LAST:event_BtnPrimeiroActionPerformed

    private void BtnAnteriorBtnPrimeiroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnAnteriorBtnPrimeiroActionPerformed
        int opcao = -1;
        int CodAtual = 0;
        if (evt.getSource() == BtnPrimeiro) {
            opcao = 0;
        } else if (evt.getSource() == BtnAnterior) {
            opcao = 1;
        } else if (evt.getSource() == BtnProximo) {
            opcao = 2;
        } else if (evt.getSource() == BtnUltimo) {
            opcao = 3;
        }
        try {
            CodAtual = Integer.parseInt(TxtCodigo.getText());
        } catch (Exception e) {
            CodAtual = 0;
        }
        PreencherCamposdaTela(CodAtual, opcao);
}//GEN-LAST:event_BtnAnteriorBtnPrimeiroActionPerformed

    private void BtnProximoBtnPrimeiroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnProximoBtnPrimeiroActionPerformed
        int opcao = -1;
        int CodAtual = 0;
        if (evt.getSource() == BtnPrimeiro) {
            opcao = 0;
        } else if (evt.getSource() == BtnAnterior) {
            opcao = 1;
        } else if (evt.getSource() == BtnProximo) {
            opcao = 2;
        } else if (evt.getSource() == BtnUltimo) {
            opcao = 3;
        }
        try {
            CodAtual = Integer.parseInt(TxtCodigo.getText());
        } catch (Exception e) {
            CodAtual = 0;
        }
        PreencherCamposdaTela(CodAtual, opcao);
}//GEN-LAST:event_BtnProximoBtnPrimeiroActionPerformed

    private void BtnUltimoBtnPrimeiroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnUltimoBtnPrimeiroActionPerformed
        int opcao = -1;
        int CodAtual = 0;
        if (evt.getSource() == BtnPrimeiro) {
            opcao = 0;
        } else if (evt.getSource() == BtnAnterior) {
            opcao = 1;
        } else if (evt.getSource() == BtnProximo) {
            opcao = 2;
        } else if (evt.getSource() == BtnUltimo) {
            opcao = 3;
        }
        try {
            CodAtual = Integer.parseInt(TxtCodigo.getText());
        } catch (Exception e) {
            CodAtual = 0;
        }
        PreencherCamposdaTela(CodAtual, opcao);

}//GEN-LAST:event_BtnUltimoBtnPrimeiroActionPerformed

    private void TxtNomeCliKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TxtNomeCliKeyPressed
        if (!BtnCancelar.isEnabled()) {
            TrocaEstado(false);
        }
}//GEN-LAST:event_TxtNomeCliKeyPressed

    private void BtnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnNovoActionPerformed

        LimparTela();
        TrocaEstado(false);                // TODO add your handling code here:
}//GEN-LAST:event_BtnNovoActionPerformed

    private void BtnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnSalvarActionPerformed
        int CodAtual = Integer.parseInt(TxtCodigo.getText());
        
        
        for (int i = 0; i < jTable1.getRowCount(); i++) {
            ArrayList<String> Registro = new ArrayList<String>();
            Registro.add(String.valueOf(CodAtual));
            Registro.add(TxtCodCli.getText());
            Registro.add(jTable1.getValueAt(i, 0).toString());
            JOptionPane.showMessageDialog(null, Registro.get(i));
            PedidosCtr objPedidos = new PedidosCtr();
            int CodNovo = 0;
            if (CodAtual > 0) {
                objPedidos.Atualizar(Registro);
            } else {
                CodNovo = objPedidos.Salvar(Registro, i);
                TxtCodigo.setText(String.valueOf(CodNovo));
            }

        }
        TrocaEstado(true);
        // TODO add your handling code here:
}//GEN-LAST:event_BtnSalvarActionPerformed

    private void BtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCancelarActionPerformed
        int CodAtual = Integer.parseInt(TxtCodigo.getText());
        if (CodAtual > 0) {
            PreencherCamposdaTela(CodAtual);
        } else {
            LimparTela();
        }

        TrocaEstado(true);// TODO add your handling code here:
}//GEN-LAST:event_BtnCancelarActionPerformed

    private void BtnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnExcluirActionPerformed
        int CodAtual = Integer.parseInt(TxtCodigo.getText());
        int iConf = JOptionPane.showConfirmDialog(null, "Tem certeza que deseja excluir o produto?", "Cadastro de produto", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (iConf == 0) {
            PedidosCtr objProdutoCtr = new PedidosCtr();
            objProdutoCtr.Excluir(CodAtual);
            JOptionPane.showMessageDialog(null, "Produto Excluído");
            LimparTela();
        } else {
            TxtCodigo.requestFocus();
        }
        TrocaEstado(true);
}//GEN-LAST:event_BtnExcluirActionPerformed

    private void BtnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnPesquisarActionPerformed
        //  new ViewPesquisarProduto(null, true, this).setVisible(true);
}//GEN-LAST:event_BtnPesquisarActionPerformed

    private void TxtCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TxtCodigoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TxtCodigoActionPerformed

    private void jTable1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyPressed
                  
        if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
            Vector<String> vColunas = new Vector<String>();
            vColunas = PegaColunasDaGrade();
            DefaultTableModel objTabela = new DefaultTableModel(vColunas, 0);
            objTabela = (DefaultTableModel) jTable1.getModel();
            Vector<String> Linha = new Vector<String>();
            Linha.add(null);
            Linha.add(null);
            Linha.add(null);
            Linha.add(null);

            objTabela.addRow(Linha);
            jTable1.setModel(objTabela);
            // jTable1.getSelectionModel().getMaxSelectionIndex();
        }
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            int linha = jTable1.getSelectedRow();
            ArrayList<String> Registro = new ArrayList<String>();
            ProdutoCtr objProdutoCtr = new ProdutoCtr();

            if (jTable1.getValueAt(linha, 0).toString() == "") {
                JOptionPane.showMessageDialog(null, "fodaaaaa");
            } else {
                int cod = (Integer.parseInt(jTable1.getValueAt(linha, 0).toString()));
                Registro = objProdutoCtr.RecuperaObjeto(cod);
                Vector<String> vColunas = new Vector<String>();
                vColunas = PegaColunasDaGrade();
                DefaultTableModel objTabela = new DefaultTableModel(vColunas, 0);

                objTabela = (DefaultTableModel) jTable1.getModel();
                objTabela.setValueAt(Registro.get(0), linha, 0);
                objTabela.setValueAt(Registro.get(1), linha, 1);
                objTabela.setValueAt(Registro.get(2), linha, 2);

                jTable1.setModel(objTabela);

            }
        }
    }//GEN-LAST:event_jTable1KeyPressed

    private void TxtCodCliFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_TxtCodCliFocusLost
        ClienteCtr objClienteCtr = new ClienteCtr();
        ArrayList<String> Registro = new ArrayList<String>();
        Registro = objClienteCtr.RecuperaObjeto(Integer.parseInt(TxtCodCli.getText()));
        TxtNomeCli.setText(Registro.get(1));
        TxtNomeCli.requestFocus();


    }//GEN-LAST:event_TxtCodCliFocusLost

    private void TxtCodCliKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TxtCodCliKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            jTable1.requestFocus();
        }
    }//GEN-LAST:event_TxtCodCliKeyPressed

    private void jTable1AncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_jTable1AncestorAdded
          int linha = jTable1.getEditingRow();
       if (linha!= -1){
           TrocaEstado(false);
       }     
    }//GEN-LAST:event_jTable1AncestorAdded

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
             // TODO add your handling code here:
    }//GEN-LAST:event_jTable1MouseClicked

    private void jTable1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MousePressed
        if (jTable1.getCellEditor()!= null) {
            TrocaEstado(false);
        }         // TODO add your handling code here:
    }//GEN-LAST:event_jTable1MousePressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ViewPedidos dialog = new ViewPedidos(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnAnterior;
    private javax.swing.JButton BtnCancelar;
    private javax.swing.JButton BtnExcluir;
    private javax.swing.JButton BtnNovo;
    private javax.swing.JButton BtnPesquisar;
    private javax.swing.JButton BtnPrimeiro;
    private javax.swing.JButton BtnProximo;
    private javax.swing.JButton BtnSalvar;
    private javax.swing.JButton BtnUltimo;
    private javax.swing.JTextField TxtCodCli;
    private javax.swing.JTextField TxtCodigo;
    private javax.swing.JTextField TxtNomeCli;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    private Vector<String> PegaColunasDaGrade() {
        Vector<String> ColunasTabela = new Vector<String>();
        ColunasTabela.add("Código");
        ColunasTabela.add("Nome");
        ColunasTabela.add("Valor");
        return ColunasTabela;
    }

    private void PreencherCamposdaTela(int CodAtual) {
        PreencherCamposdaTela(CodAtual, -1);
    }

    private void PreencherCamposdaTela(int CodAtual, int op) {
        PedidosCtr objClienteCtr = new PedidosCtr();
        ArrayList<String> Registro = new ArrayList<String>();

        if (op == -1) {
            Registro = objClienteCtr.RecuperaObjeto(CodAtual);
        } else {
            Registro = objClienteCtr.RecuperaObjetoNaveg(op, CodAtual);
        }

        if (!Registro.get(0).equals("-1")) {
            if (op != -1) {
                TxtCodigo.setText(String.valueOf(Registro.get(0)));
            }
            TxtCodCli.setText(Registro.get(1));
            TxtNomeCli.setText(Registro.get(2));
            Vector<String> vColunas = new Vector<String>();
            vColunas = PegaColunasDaGrade();
            DefaultTableModel objTabela = new DefaultTableModel(vColunas, 0);

            Vector<String> vetVetor = new Vector<String>();
            vetVetor.add(Registro.get(3));
            vetVetor.add(Registro.get(4));
            vetVetor.add(Registro.get(5));

            objTabela.addRow(vetVetor);
            jTable1.setModel(objTabela);

            // CbCidade.setSelectedItem(Registro.get(6));
        } else {
            if (op == -1) {
                JOptionPane.showMessageDialog(null, "Dados não encontrados");
            }
        }
        TrocaEstado(true);
    }

    private void TrocaEstado(boolean habilitar) {
        Component[] c;

        BtnNovo.setEnabled(habilitar);
        BtnSalvar.setEnabled(!habilitar);
        BtnExcluir.setEnabled(habilitar);
        BtnCancelar.setEnabled(!habilitar);
        BtnPesquisar.setEnabled(habilitar);
        c = jPanel1.getComponents();
        for (int i = 0; i < c.length; i++) {
            c[i].setEnabled(habilitar);
        }
    }

    private void LimparTela() {
        TxtCodigo.setText("0");
        TxtCodCli.setText("");
        TxtNomeCli.setText("");
        Vector<String> ColunasTabela = new Vector<String>();
        ColunasTabela = PegaColunasDaGrade();
        DefaultTableModel MinhaTabela = new DefaultTableModel(ColunasTabela, 1);
        jTable1.setModel(MinhaTabela);

    }
}
